<template>
  <div class="checkbox">
    <label :class="{checked: isChecked, disabled: disabled}" :for="id"
           @click.stop.prevent="onChange">
      <input
        ref="input"
        type="checkbox"
        :name="field.name"
        :checked="isChecked"
        :disabled="disabled" :id="id">
      {{isChecked ? trueText : falseText}}
    </label>
  </div>
</template>

<script>
  import _ from 'lodash'

  let checkboxId = 0

  export default {
    props: {
      schema: {
        type: Object,
        default () {
          return {}
        }
      },
      value: {},
      field: Object,
      disabled: Boolean
    },
    mounted () {
      this.id = `dyf_checkbox_${++checkboxId}`
    },
    data () {
      return {
        id: 0
      }
    },
    methods: {
      onChange (e) {
        if (this.disabled) {
          return
        }
        let val
        if (!this.$refs.input.checked) {
          if (this.field.trueValue !== undefined) {
            val = this.field.trueValue
          } else {
            val = true
          }
        } else {
          if (this.field.falseValue !== undefined) {
            val = this.field.falseValue
          } else {
            val = false
          }
        }
        this.$emit('input', val, e)
      }
    },
    computed: {
      isChecked () {
        if (this.field.trueValue !== undefined) {
          return this.field.trueValue === this.value
        }
        if (this.field.falseValue !== undefined) {
          return this.field.falseValue !== this.value
        }
        return !!this.value
      },
      trueText () {
        return _.isNil(this.field.trueText) ? this.field.defaultText || '' : this.field.trueText
      },
      falseText () {
        return _.isNil(this.field.falseText) ? this.field.defaultText || '' : this.field.falseText
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "../css/variables";

  @mixin checkbox-variant($parent, $color) {
    .checkbox#{$parent} label{
      &:before{
        background: $color;
        border-color: darken($color, 10%);
      }
      &.checked:after{
        color: #fff;
      }
    }
  }

  .checkbox{
    display: inline-block;
    label{
      position: relative;
      display: inline-block;
      min-height: 20px;
      padding-left: 22px;
      margin-bottom: 0;
      font-weight: normal;
      cursor: pointer;

      &.disabled{
        opacity: .6;
        cursor: not-allowed;
      }
    }
    label:before{
      content: ' ';
      position: absolute;
      left: 3px;
      top: 2px;
      color: transparent;
      height: 16px;
      width: 16px;
      background: #fff;
      display: inline-block;
      border: 1px solid #ccc;
    }
    label:after{
      content: ' ';
      position: absolute;
      left: 0;
      top: 0;
      width: 22px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      color: transparent;
      font-size: 12px;
      display: inline-block;
    }
    label.checked:after{
      content: '\F00C';
      color: #333;
      font-family: FontAwesome;
    }

    input{
      display: none;
      visibility: hidden;
    }
  }

  @include checkbox-variant('.checkbox-primary', $brand-primary);
  @include checkbox-variant('.checkbox-danger', $brand-danger);
  @include checkbox-variant('.checkbox-info', $brand-info);
  @include checkbox-variant('.checkbox-warning', $brand-warning);
  @include checkbox-variant('.checkbox-success', $brand-success);
</style>
